package org.example.kidscode.service.impl;

import org.example.kidscode.entity.Teacher;
import org.example.kidscode.repository.TeacherRepository;
import org.example.kidscode.service.TeacherService;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    private final TeacherRepository teacherRepository;

    public TeacherServiceImpl(TeacherRepository teacherRepository) {
        this.teacherRepository = teacherRepository;
    }

    @Override
    public Teacher saveTeacher(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    @Override
    public Teacher getTeacherById(String teacherId) {
        return teacherRepository.findById(teacherId)
                .orElseThrow(() -> new RuntimeException("Teacher not found with id: " + teacherId));
    }

    @Override
    public List<Teacher> getAllTeachers() {
        return teacherRepository.findAll();
    }

    @Override
    public Teacher updateTeacher(String teacherId, Teacher teacherDetails) {
        Teacher teacher = getTeacherById(teacherId);

        teacher.setName(teacherDetails.getName());
        teacher.setGender(teacherDetails.getGender());
        teacher.setContactPhone(teacherDetails.getContactPhone());
        teacher.setEmail(teacherDetails.getEmail());
        teacher.setSpecialty(teacherDetails.getSpecialty());
        teacher.setHireDate(teacherDetails.getHireDate());
        teacher.setStatus(teacherDetails.getStatus());

        return teacherRepository.save(teacher);
    }

    @Override
    public void deleteTeacher(String teacherId) {
        Teacher teacher = getTeacherById(teacherId);
        teacherRepository.delete(teacher);
    }

    @Override
    public boolean isPhoneNumberExists(String contactPhone) {
        return teacherRepository.existsByContactPhone(contactPhone);
    }
}